package best.driver.android.Activity.Finance;


import android.appwidget.AppWidgetHost;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.AppConfig;
import best.driver.android.AppInfo;
import best.driver.android.Event.WithdrawEvent;
import best.driver.android.R;
import best.driver.android.RequestFactory;
import best.driver.android.Utils.TimeUtil;
import best.driver.android.Utils.Util;
import best.driver.android.common.app.AppProxyFactory;
import best.driver.android.common.network.ApiRequest;
import best.driver.android.common.network.ApiStringRequest;
import best.driver.android.common.network.BestApiError;
import best.driver.android.common.network.BestError;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class WithdrawActivity extends BaseActivity {

    private double mValidMoney;

    @InjectView(R.id.tv_valid_money)
    TextView mTvValidMoney;

    @InjectView(R.id.edt_money)
    EditText mEdtMoney;

    @InjectView(R.id.tv_time)
    TextView mTvTime;

    @InjectView(R.id.btn_withdraw)
    Button mBtnWithdraw;

    @OnClick(R.id.btn_withdraw)
    protected void withdraw() {
        mBtnWithdraw.setEnabled(false);
        String content = mEdtMoney.getText().toString().trim();
        if(TextUtils.isEmpty(content) || Double.valueOf(content) < 0.01) {
            AppInfo.showToast(WithdrawActivity.this, "请输入提现金额");
            mBtnWithdraw.setEnabled(true);
            return;
        }
        showWaiting();
        final double money = Double.valueOf(content);
        ApiStringRequest request = RequestFactory.createWithdrawRequest(money);
        request.setListener(new ApiRequest.ApiRequestListener<String>() {
            @Override
            public void onRequestSuccess(String response) {
                mValidMoney = mValidMoney - money;
                mTvValidMoney.setText(Util.getOnePointNumber(mValidMoney) + "元");
                EventBus.getDefault().post(new WithdrawEvent(money));
                AppInfo.showToast(WithdrawActivity.this, "提现申请成功");
                mBtnWithdraw.setEnabled(true);
                hideWaiting();
                WithdrawActivity.this.finish();
            }

            @Override
            public void onRequestError(BestError error) {
                if(error instanceof BestApiError) {
                    AppInfo.showToast(WithdrawActivity.this, ((BestApiError) error).getErrorMessage());
                } else {
                    AppInfo.showToast(WithdrawActivity.this, "请重试");
                }
                mBtnWithdraw.setEnabled(true);
                hideWaiting();
            }
        });
        AppProxyFactory.getProxy().getNetworkManager().enqueueRequest(request);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_withdraw);
        ButterKnife.inject(this, view);
        setLeftIvBak(R.drawable.icon_navibar_back);
        mValidMoney = getIntent().getDoubleExtra("money", 0.0);
        mTvValidMoney.setText(Util.getOnePointNumber(mValidMoney) + "元");
        long time = getIntent().getLongExtra("time", 0);
        if(time > 0) {
            mTvTime.setText("(" + TimeUtil.convertToYMD(time)+ ")");
        }
        setTopTopic("提现");
        mEdtMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String content = mEdtMoney.getText().toString().trim();
                if(content != null && content.length() > 0) {
                    Double money = Double.valueOf(content);
                    if(money > (mValidMoney + 0.008)) {
                        AppInfo.showToast(WithdrawActivity.this, "超过最大提现额度");
                        mEdtMoney.setText(Util.getOnePointNumber(mValidMoney));
                        int length = Util.getOnePointNumber(mValidMoney).length();
                        mEdtMoney.setSelection(length);
                        return;
                    }
                    String[] strings = content.split("\\.");
                    if(strings.length == 2) {
                        if(strings[1].length() > 2) {
                            AppInfo.showToast(WithdrawActivity.this,"小数点后至多输入两位");
                            mEdtMoney.setText(Util.getOnePointNumber(money));
                            int length = Util.getOnePointNumber(money).length();
                            mEdtMoney.setSelection(length);
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }



    @Override
    protected void onBack() {
        this.finish();
    }

}
